package com.neusoft.resturant.controller;


import com.alibaba.fastjson2.JSON;

import com.neusoft.resturant.entity.ResCar;
import com.neusoft.resturant.entity.SysUser;
import com.neusoft.resturant.service.IResCarService;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;


/**
 * 购物车Controller
 * 
 * @author ruoyi
 * @date 2023-12-09
 */
@RestController
@RequestMapping("/restaurant/car")
public class ResCarController extends BaseController
{
    @Autowired
    private IResCarService resCarService;




    /**
     * 获取购物车详细信息
     */
    @GetMapping("/{id}")
    public ResCar getInfo(@PathVariable(value = "id") Long carId) {
        System.out.println("id=" + carId);
        ResCar car = resCarService.selectResCarById(carId);
        return car;
    }
    /**
     * 新增购物车
     */
    @PostMapping("/add")
    public int add(@RequestBody ResCar resCar)
    {
        String token = request.getHeader("token");
        // 从Redis中获取use_id
        String  userJson = (String) redisTemplate.opsForValue().get(token);
        SysUser sysUser = JSON.parseObject(userJson, SysUser.class);
        Long userId=sysUser.getUserId();
        String createBy=sysUser.getUserName();
        // 如果use_id为空，则返回错误或采取其他适当的操作
        if (userId == null ||createBy==null) {
            return -1; // 或者其他表示错误的值
        }

        // 将use_id设置到resComment对象中
        resCar.setUserId(Long.valueOf(userId));
        resCar.setCreateBy(createBy);
        System.out.println("新增成功");
        return (resCarService.insertResCar(resCar));
    }
    /**
     * 修改购物车
     */
    @PutMapping("/edit")
    public int edit(@RequestBody ResCar resCar)
    {
        String token = request.getHeader("token");
        // 从Redis中获取use_id
        String  userJson = (String) redisTemplate.opsForValue().get(token);
        SysUser sysUser = JSON.parseObject(userJson, SysUser.class);

        String updateBy=sysUser.getUserName();
        // 如果use_id为空，则返回错误或采取其他适当的操作
        if (updateBy==null) {
            return -1; // 或者其他表示错误的值
        }

        // 将updateBy设置到resComment对象中
        resCar.setUpdateBy(updateBy);
        System.out.println("修改成功");
        return (resCarService.updateResCar(resCar));
    }
    /**
     * 删除购物车
     */
    @DeleteMapping("/remove/{id}")
    public int remove(@PathVariable Long[] id)
    {
        return (resCarService.deleteResCarByIds(id));
    }
}
